//
//  UIStandardArrowLabel.m
//  vhECGTools
//
//  Created by jia yu on 2022/6/29.
//

#import "UIStandardArrowLabel.h"

@interface UIStandardArrowLabel(){
    CGMutablePathRef path;
    CAShapeLayer *bgLayer;
}
@end

@implementation UIStandardArrowLabel
@synthesize style;
@synthesize textLabel;
@synthesize bgColor;
@synthesize bgWidth;
@synthesize radius;

-(void)refresh{
    if (path){
        path = nil;
    }
    if (bgLayer){
        [bgLayer removeFromSuperlayer];
        bgLayer = nil;
    }
    [self setNeedsDisplay];
}

#pragma mark - Set
-(void)setBgColor:(UIColor *)_bgColor{
    bgColor = _bgColor;
    [self refresh];
    return;
}

-(void)setBgWidth:(float)_bgWidth{
    bgWidth = _bgWidth;
    [self refresh];
    return;
}

-(void)setRadius:(float)_radius{
    radius = _radius;
    [self refresh];
    return;
}

#pragma mark - System
-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self){
        self.backgroundColor = [UIColor clearColor];
        bgColor = [UIColor orangeColor];
        bgWidth = frame.size.width * 0.9;
        radius = frame.size.height * 0.1;
        textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.f, 0.f, bgWidth, frame.size.height)];
        textLabel.backgroundColor = [UIColor clearColor];
        textLabel.text = @"UIStandardArrowLabel";
        textLabel.textColor = [UIColor whiteColor];
        textLabel.textAlignment = NSTextAlignmentCenter;
        textLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:frame.size.height * 0.9f];
        [self addSubview:textLabel];
    }
    return self;
}

-(void)setFrame:(CGRect)frame{
    [super setFrame:frame];
    [self refresh];
}

-(void)drawRect:(CGRect)rect{
    [super drawRect:rect];
    
    if (UIStandardArrowLabel_Style_Qiao == style){
        [textLabel setFrame:CGRectMake(0.f, 0.f, self.frame.size.width, self.frame.size.height)];
    }else{
        [textLabel setFrame:CGRectMake(0.f, 0.f, bgWidth, self.frame.size.height)];
    }

    if (nil == path){
        path = CGPathCreateMutable();
        float height = self.frame.size.height;
        
        CGPathMoveToPoint(path, nil, 0.f, radius);
        //左上圆角
        CGPathAddArcToPoint(path, nil, 0.f, 0.f, radius, 0.f, radius);
        CGPathAddLineToPoint(path, nil, bgWidth-radius, 0.f);
        
        if (UIStandardArrowLabel_Style_Default == style){
            //右上圆角
            CGPathAddArcToPoint(path, nil, bgWidth, 0.f, bgWidth, radius, radius);
            CGPathAddLineToPoint(path, nil, bgWidth, height * 0.31f);
            CGPathAddLineToPoint(path, nil, self.frame.size.width, height*0.51f);
            CGPathAddLineToPoint(path, nil, self.frame.size.width, height*0.55f);
            CGPathAddLineToPoint(path, nil, bgWidth, height * 0.73f);
            CGPathAddLineToPoint(path, nil, bgWidth, height - radius);
            
            //右下圆角
            CGPathAddArcToPoint(path, nil, bgWidth, height, bgWidth-radius, height, radius);
            CGPathAddLineToPoint(path, nil, radius, height);
        }
        
        if (UIStandardArrowLabel_Style_Qiao == style){
            CGPathAddLineToPoint(path, nil, bgWidth, 0.f);
            CGPathAddLineToPoint(path, nil, self.frame.size.width, height * 0.44f);
            CGPathAddLineToPoint(path, nil, self.frame.size.width, height * 0.56f);
            CGPathAddLineToPoint(path, nil, bgWidth, height);
        }
        //左下圆角
        CGPathAddArcToPoint(path, nil, 0.f, height, 0.f, height-radius, radius);
        CGPathAddLineToPoint(path, nil, 0.f, radius);
        CGPathCloseSubpath(path);
    }
    
    if (bgLayer){
        [bgLayer removeFromSuperlayer];
    }
    bgLayer = [CAShapeLayer layer];
    bgLayer.fillColor = bgColor.CGColor;
    bgLayer.path = path;
    [bgLayer fillColor];
    [self.layer addSublayer:bgLayer];
    [self bringSubviewToFront:textLabel];
    return;
}

-(void)dealloc{
    if (path){
        CFRelease(path);
        path = nil;
    }
}
@end
